package cn.message.service.impl;

import cn.itcast.mapper.horsemanMapper;
import cn.itcast.pojo.horseman;
import cn.itcast.pojo.horsemanExample;
import cn.itcast.pojo.user;
import cn.itcast.pojo.userExample;
import cn.message.service.IHorsemanService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 郭智超
 * @create 2020-03-25 16:10
 */
@Slf4j
@Service("horsemanService")
public class HorsemanServiceImpl implements IHorsemanService {

    @Resource
    private horsemanMapper horsemanmapper;

    @Resource
    private cn.itcast.mapper.userMapper userMapper;


    @Override
    /**
     * 获取所有符合查询条件的骑手的信息
     */
    public Map<String, Object> getAllHorseMan(Integer page, Integer limit, horseman horseman) {
        horsemanExample hEx = new horsemanExample();
        horsemanExample.Criteria criteria = hEx.createCriteria();

        //添加查询条件
        //根据骑手性别模糊查询
        if(horseman.getGender() != null){
            criteria.andGenderEqualTo(horseman.getGender());
        }

        //根据骑手姓名模糊查询
        if(!horseman.getHorsemanName().isEmpty()){
            criteria.andHorsemanNameLike("%"+horseman.getHorsemanName()+"%");
        }

        //根据骑手所属区域模糊查询
        if(!horseman.getHorsemanArea().isEmpty()){
            criteria.andHorsemanAreaLike("%"+horseman.getHorsemanArea()+"%");
        }

        //根据骑手登录用户名模糊查询
        if(!horseman.getHorsemanNumber().isEmpty()){
            criteria.andHorsemanNumberLike("%"+horseman.getHorsemanNumber()+"%");
        }

        //根据骑手星级模糊查询
        if(!horseman.getHorsemanStarLevel().isEmpty()){
            criteria.andHorsemanStarLevelEqualTo(horseman.getHorsemanStarLevel());
        }

        //根据所属商家手机号
        if(!horseman.getHorsemanReferrer().isEmpty()){
            criteria.andHorsemanReferrerLike("%"+horseman.getHorsemanReferrer()+"%");
        }

        //返回符合查询条件的总记录数
        int count = horsemanmapper.countByExample(hEx);

        //分页
        PageHelper.startPage(page, limit);
        //查询所有符合条件的骑手信息
        List<horseman> horsemanList = horsemanmapper.selectByExample(hEx);
        //格式化日期
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (horseman horseman2 : horsemanList) {
            if(horseman2.getLastVisitTime() != null){
                horseman2.setLastVisitTime_String(sdf.format(horseman2.getLastVisitTime()));
            }
        }

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("count",count);
        map.put("horsemanList",horsemanList);

        return map;
    }


    @Override
    /**
     * 获取所有符合查询条件的用户的信息
     */
    public Map<String, Object> getAllUser(Map<String,String> map) {
       userExample hEx = new userExample();
       userExample.Criteria criteria = hEx.createCriteria();
        //添加查询条件
        //根据用户性别查询
        if(!map.get("gender").isEmpty()){
            criteria.andGenderEqualTo(Byte.parseByte(map.get("gender")));
        }
        //根据用户姓名模糊查询
        if(!map.get("userName").isEmpty()){
            criteria.andUserNameLike("%"+map.get("userName")+"%");
        }
        //根据用户微信名模糊查询
        if(!map.get("nickName").isEmpty()){
            criteria.andNickNameLike("%"+map.get("nickName")+"%");
        }

        //返回符合查询条件的总记录数
        int count = userMapper.countByExample(hEx);

        Integer page = Integer.parseInt(map.get("page"));
        Integer limit = Integer.parseInt(map.get("limit"));
        //分页
        PageHelper.startPage(page, limit);
        //查询所有符合条件的骑手信息
        List<user> userList = userMapper.selectByExample(hEx);
        //格式化日期
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (user user : userList) {
            if(user.getLastVisitTime() != null){
                user.setLastVisitTime_String(sdf.format(user.getLastVisitTime()));
            }
        }

        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("count",count);
        resultMap.put("userList",userList);

        return resultMap;
    }

    /**
     * 增加骑手
     * @param horseman
     * @return
     */
    @Override
    public Boolean addHorseman(horseman horseman) {
        int i = horsemanmapper.insertSelective(horseman);
        if (i!=1){
            return false;
        }
        return true;
    }

    /**
     * 根据骑手id删除骑手
     * @param horsemanId
     * @return
     */
    @Override
    public Boolean deleteHorsemanByHorsemanId(int horsemanId) {
        int i = horsemanmapper.deleteByPrimaryKey(horsemanId);
        if(i!=1){
            return false;
        }
        return true;
    }

    /**
     * 根据骑手id修改骑手信息
     * @param horseman
     * @return
     */
    @Override
    public Boolean updateHorseman(horseman horseman) {
        int i = horsemanmapper.updateByPrimaryKeySelective(horseman);
        System.out.println(i);
        if (i != 1){
            return false;
        }
        return true;
    }

    /**
     * 判断输入的用户名是否已经存在，存在则返回true，不存在返回false
     * @param horsemanNumber
     * @return
     */
    @Override
    public Boolean isExist(String horsemanNumber) {
        horsemanExample horsemanExample = new horsemanExample();
        cn.itcast.pojo.horsemanExample.Criteria criteria = horsemanExample.createCriteria();
        criteria.andHorsemanNumberEqualTo(horsemanNumber);
        List<horseman> horsemanList = horsemanmapper.selectByExample(horsemanExample);
        if(horsemanList.size() == 0){
            return false;
        }
        return true;
    }

    @Override
    public List<horseman> selectHorsemanphone(String userPhone) {
        List<horseman> horsemen = horsemanmapper.selectHorsemanphone(userPhone);
        return horsemen;
    }

    @Override
    public List<horseman> selectAllHorsemanLatAndLon() {
        List<horseman> horsemen = horsemanmapper.selectAllHorsemanLatAndLon();
        return horsemen;
    }

    public List<horseman> selectHorsemanOpenId(String openId){
        return horsemanmapper.selectHorsemanOpenId(openId);
    }
}
